
package com.kugou.game.sdk.adapter;

import com.kugou.game.sdk.base.BaseListAdapter;
import com.kugou.game.sdk.entity.Coupon;
import com.kugou.game.sdk.ui.activity.RechargeActivity;
import com.kugou.game.sdk.ui.activity.UserCenterActivity;
import com.kugou.game.sdk.ui.dialog.BaseConfirmDialog;
import com.kugou.game.sdk.utils.AppUtil;
import com.kugou.game.sdk.utils.Rx;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 描述: 代金券列表适配器
 *
 * @author jamywang
 * @since 2016年5月16日 上午11:31:29
 */
public class CouponListAdapter extends BaseListAdapter<Coupon> implements OnClickListener {

    private int listType = 0;// 列表类型,0:可用代金券;1:历史代金券

    public CouponListAdapter(Context context, ViewGroup viewGroup) {
        super(context, viewGroup);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(Rx.layout.kg_coupon_list_item, null);
            holder = new ViewHolder();
            holder.background = (LinearLayout) convertView
                    .findViewById(Rx.id.kg_item_coupon_background);
            holder.tvCouponMoney = (TextView) convertView
                    .findViewById(Rx.id.kg_tv_item_coupon_money);
            holder.tvMoneyUnit = (TextView) convertView
                    .findViewById(Rx.id.kg_tv_item_coupon_money_unit);
            holder.tvCouponName = (TextView) convertView.findViewById(Rx.id.kg_tv_item_coupon_name);
            holder.tvPlatfrom = (TextView) convertView
                    .findViewById(Rx.id.kg_tv_item_coupon_platfrom);
            holder.tvEndDate = (TextView) convertView.findViewById(Rx.id.kg_tv_item_coupon_date);
            holder.tvStatus = (TextView) convertView.findViewById(Rx.id.kg_tv_item_coupon_status);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Coupon coupon = mDatas.get(position);
        if (listType == 0) {
            holder.background.setBackgroundResource(Rx.drawable.kg_coupon_item_usable);
            holder.background.setTag(coupon);
            holder.background.setOnClickListener(this);
            holder.tvStatus.setVisibility(View.GONE);
            holder.tvCouponMoney.setText("" + coupon.getMoney());
            holder.tvCouponMoney.setTextColor(mContext.getResources().getColor(
                    Rx.color.kg_common_orange));
            holder.tvMoneyUnit.setTextColor(mContext.getResources().getColor(
                    Rx.color.kg_common_orange));
            holder.tvCouponName.setText(coupon.getCouponName());
            holder.tvCouponName.setTextColor(mContext.getResources().getColor(
                    Rx.color.kg_common_black));
            if (coupon.getType() == 2) {
                holder.tvPlatfrom.setText("限游币钱包充值");
            }
            if (coupon.getType() == 1) {
                if (coupon.getGames() != null && (coupon.getGames()).size() == 0) {
                    holder.tvPlatfrom.setText("限安卓游戏");
                }
                if (coupon.getGames() != null && (coupon.getGames()).size() == 1) {
                    holder.tvPlatfrom.setText("限《" + coupon.getGames().get(0) + "》安卓版");
                }
                if (coupon.getGames() != null && (coupon.getGames()).size() > 1) {
                    holder.tvPlatfrom.setText("限《" + coupon.getGames().get(0) + "》等游戏");
                }
            }
            if (coupon.getLockStatus() == Coupon.COUPON_STATUS_LOCK) {
                holder.tvEndDate.setText("订单提交中，" + AppUtil.getDate("HH点mm分", coupon.getLockTime())
                        + "后可用");
            } else {
                holder.tvEndDate.setText("截止期："
                        + AppUtil.getDate("yyyy.MM.dd", coupon.getEndtime()));
            }
            holder.tvStatus.setText("");
        } else if (listType == 1) {
            holder.background.setBackgroundResource(Rx.drawable.kg_coupon_item_history);
            holder.background.setOnClickListener(null);
            holder.tvStatus.setVisibility(View.VISIBLE);
            holder.tvCouponMoney.setText("" + coupon.getMoney());
            holder.tvCouponMoney.setTextColor(mContext.getResources().getColor(
                    Rx.color.kg_common_gray_999999));
            holder.tvMoneyUnit.setTextColor(mContext.getResources().getColor(
                    Rx.color.kg_common_gray_999999));
            holder.tvCouponName.setText(coupon.getCouponName());
            holder.tvCouponName.setTextColor(mContext.getResources().getColor(
                    Rx.color.kg_common_gray_999999));
            if (coupon.getType() == 2) {
                holder.tvPlatfrom.setText("限游币钱包充值");
            }
            if (coupon.getType() == 1) {
                if (coupon.getGames() != null && (coupon.getGames()).size() == 0) {
                    holder.tvPlatfrom.setText("限安卓游戏");
                }
                if (coupon.getGames() != null && (coupon.getGames()).size() == 1) {
                    holder.tvPlatfrom.setText("限《" + coupon.getGames().get(0) + "》安卓版");
                }
                if (coupon.getGames() != null && (coupon.getGames()).size() > 1) {
                    holder.tvPlatfrom.setText("限《" + coupon.getGames().get(0) + "》等游戏");
                }
            }
            holder.tvEndDate.setText("截止期：" + AppUtil.getDate("yyyy.MM.dd", coupon.getEndtime()));
            if (coupon.getStatus() == 2) {
                holder.tvStatus.setText("已使用");
            } else if (coupon.getStatus() == 10) {
                holder.tvStatus.setText("已过期");
            } else {
                holder.tvStatus.setText("");
            }
        }

        return convertView;
    }

    public static class ViewHolder {

        // 代金券背景
        public LinearLayout background;

        // 代金券金额
        public TextView tvCouponMoney;

        // 代金券金额单位
        public TextView tvMoneyUnit;

        // 代金券名称
        public TextView tvCouponName;

        // 适用平台
        public TextView tvPlatfrom;

        // 截止日期
        public TextView tvEndDate;

        // 代金券状态
        public TextView tvStatus;
    }

    @Override
    public Coupon getItem(int position) {
        if (mDatas != null && position >= 0 && position < mDatas.size()) {
            return mDatas.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return mDatas.get(position).getId();
    }

    public int getListType() {
        return listType;
    }

    public void setListType(int listType) {
        this.listType = listType;
    }

    @Override
    public void onClick(View v) {
        Coupon couponInfo = (Coupon) v.getTag();
        int payType = couponInfo.getType();
        String tips = "";
        // 游戏代金券
        if (payType == 1) {
            // 所有游戏都可使用的代金券
            if (couponInfo.getGames() != null && (couponInfo.getGames()).size() == 0) {
                tips = "该代金券适用于酷狗游戏中心下载的所有安卓游戏，请尽快在游戏中支付使用哦。";
                showNoticeDialog(tips);
            }
            // 部分游戏可使用的代金券
            else if (couponInfo.getGames() != null && (couponInfo.getGames()).size() > 0) {
                String str = "";
                for (int i = 0; i < couponInfo.getGames().size(); i++) {
                    str += "《" + couponInfo.getGames().get(i) + "》、";
                }
                str = str.substring(0, str.length() - 1);
                tips = "该代金券仅使用于酷狗游戏中心下载的安卓版" + str + ",请尽快在游戏中支付使用哦。";
                showNoticeDialog(tips);
            }

        }

        // 游币代金券
        if (payType == 2) {
            tips = "该代金券仅适用于酷狗游币钱包充值，请尽快到游币钱包充值使用哦。";
            showRechargeDialog(tips);
        }
    }

    /**
     * 显示代金券使用提示的确定弹框
     * 
     * @param tips 弹框Message
     */
    private void showNoticeDialog(String tips) {
        final BaseConfirmDialog tipsDialog = new BaseConfirmDialog((Activity) mContext);
        tipsDialog
                .setOkBtnBackground(Rx.drawable.kg_selector_bottom_single_filleted_corner_retangle);
        tipsDialog.setMessage(TextUtils.isEmpty(tips) ? "" : tips);
        tipsDialog.setCancelBtnVisibility(View.GONE);
        tipsDialog.setContentGravity(Gravity.CENTER);
        tipsDialog.setOKBtnText("知道了");
        tipsDialog.setOKClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                tipsDialog.dismiss();

            }
        });
        tipsDialog.show();
    }

    private void showRechargeDialog(String tips) {
        final BaseConfirmDialog dialog = new BaseConfirmDialog((Activity) mContext);
        dialog.setMessage(TextUtils.isEmpty(tips) ? "" : tips);
        dialog.setContentGravity(Gravity.CENTER);
        dialog.setOKBtnText("游币充值");
        dialog.setCancelText("关闭");
        dialog.setCancelClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setOKClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                RechargeActivity.showYoubiRechargePage(mContext, 6);
                dialog.dismiss();
            }
        });
        dialog.show();
    }

}
